package com.zp.base.admin.common.aspect;

import com.zp.base.admin.common.utils.IPKit;
import com.zp.base.admin.dao.OperatorLogDao;
import com.zp.base.admin.entity.OperatorLogEntity;
import com.zp.base.admin.entity.UserEntity;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author ZhangPeng
 * @email zhangpeng98@aliyun.com
 * @create 2021-03-02 21:06
 * 通过aop切注解方式记录日志
 */
@Component
@Aspect
public class LogAspect {

    @Autowired
    OperatorLogDao operatorLogDao;

    @Around("@annotation(com.zp.base.admin.common.aspect.AddOperatorLog)")
    public Object afterReturning(ProceedingJoinPoint joinPoint){

        OperatorLogEntity operatorLogEntity = new OperatorLogEntity();
        operatorLogEntity.setUpdateTime(new Date());
        operatorLogEntity.setCreateTime(new Date());

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //获取访问ip
        String ip = IPKit.getIpAddrByRequest(request);
        operatorLogEntity.setIp(ip);

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = null;
        if (authentication != null) {
            principal = authentication.getPrincipal();
            UserEntity entity = (UserEntity) principal;
            operatorLogEntity.setOperator(entity.getUsername());
        }
        String content = "";
        Object proceed = null;
        try {
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            AddOperatorLog annotation = signature.getMethod().getAnnotation(AddOperatorLog.class);
            content = annotation.content();

            //执行目标方法
            proceed = joinPoint.proceed(joinPoint.getArgs());

            if (principal == null) {
                Object user1 = authentication.getPrincipal();
                if (user1 != null) {
                    UserEntity entity = (UserEntity) user1;
                    operatorLogEntity.setOperator(entity.getUsername());
                }
            }
            return proceed;
        } catch (Throwable throwable) {
            content = content + " ---- 服务器错误";
            return proceed;
        } finally {
            //添加日志
            operatorLogEntity.setContent(content);
            operatorLogDao.insert(operatorLogEntity);
        }
    }
}
